package dao.imp;

import dao.DaoLogic;
import javabean.House;
import org.apache.commons.dbutils.QueryRunner;
import tools.Dbutils;
import ui.Show;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
//数据访问层
public class DaoLogicImp implements DaoLogic {
    Scanner scanner = new Scanner(System.in);
    private QueryRunner queryRunner = new QueryRunner();
    /**
     * 1.新增房源
     * @param
     */
    @Override
    public void addDao() {
        House newhouse = new House();
        System.out.println("---请输入你要添加的房屋信息---");
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入电话：");
        String tel = scanner.next();
        System.out.println("请输入地址：");
        String address = scanner.next();
        System.out.println("请输入月租");
        String rent = scanner.next();
        System.out.println("请输入状态");
        String state = scanner.next();
        int update = 0;
        try {
            update = Dbutils.update("insert into house values(null,?,?,?,?,?)", name, tel, address, rent, state);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("---成功增加"+update+"条房源---");
    }

    /**
     *2.查找房屋信息
     * @param
     * @param
     * @return
     */
    @Override
    public Boolean findDao(int id) {
        House house = Dbutils.queryBean("select * from house where id=?", House.class, id);
        if(house==null){
            return false;
        }

        System.out.println("该房屋的信息是："+house);
        return true;
    }

    /**
     * 3.删除房屋信息
     * @param
     * @param
     * @return
     */
    @Override
    public Integer delDao(int id) {
        int update = 0;
        try {
            update = Dbutils.update("delete from house where id=?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    /**
     * 4.修改房屋信息
     * @param
     * @param
     */
    @Override
    public void updateDao(int id) {

        House newHouse = new House();
        House house = Dbutils.queryBean("select * from house where id=?", House.class, id);
//        scanner.nextLine();
        System.out.println("请输入新的房屋编号：");
        String id1 = scanner.nextLine();
        if(id1.equals("")){
            newHouse.setId(house.getId());
        }else{
            Integer id2 = Integer.valueOf(id1);
            newHouse.setId(id2);
        }

//        scanner.nextLine();
        System.out.println("请输入新的姓名：");
        String name = scanner.nextLine();
        if(name.equals("")){
            newHouse.setName(house.getName());
        }else {
            newHouse.setName(name);
        }

        System.out.println("请输入新的电话：");
        String tel = scanner.nextLine();
        if(tel.equals("")){
            newHouse.setTel(house.getTel());
        }else {
            newHouse.setTel(tel);
        }

        System.out.println("请输入新的房屋地址：");
        String address = scanner.nextLine();
        if(address.equals("")){
            newHouse.setAddress(house.getAddress());
        }else {
            newHouse.setAddress(address);
        }

        System.out.println("请输入新的房屋月租：");
        String rent = scanner.nextLine();
        if(rent.equals("")){
            newHouse.setRent(house.getRent());
        }else {
            newHouse.setRent(rent);
        }

        System.out.println("请输入新的房屋状态：");
        String state = scanner.nextLine();
        if(state.equals("")){
            newHouse.setState(house.getState());
        }else {
            newHouse.setState(state);
        }
//        System.out.println("newHouse = " + newHouse);
        try {
            int update = Dbutils.update("update house set name=?,tel=?,address=?,rent=?,state=? where id=?"
                    , newHouse.getName(), newHouse.getTel(), newHouse.getAddress()
                    , newHouse.getRent(), newHouse.getState(), newHouse.getId());
//            System.out.println("newHouse.getRent() = " + newHouse.getRent());
//            System.out.println(update);
            if(update>0){
                System.out.println("---成功修改该房源信息---");
            }else{
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 5.房屋列表
     * @param
     */
    @Override
    public void listService() {//len是有效长度
        ArrayList<House> houses1 = Dbutils.queryBeanList("select * from house", House.class);
        System.out.println("编号"+"\t"+"姓名"+"\t"+"电话"+"\t"+"地址"+"\t"+"月租"+"\t"+"状态"+"\t");
        for (int i = 0; i < houses1.size(); i++) {
            System.out.println(houses1.get(i).getId()+"\t"+houses1.get(i).getName()+"\t"
                    +houses1.get(i).getTel()+"\t"+houses1.get(i).getAddress()+"\t"+houses1.get(i).getRent()
                    +"\t"+houses1.get(i).getState()+"\t");
        }
    }
}
